package javase.collections;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class SortingTester {

	private static final int LIST_SIZE = 10000000;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		ArrayList<Integer> arrayList = getRandomArrayList();
//		LinkedList<Integer> linkedList = new LinkedList<Integer>(arrayList);
		
		LinkedList<Integer> linkedList = getRandomLinkedList();
		ArrayList<Integer> arrayList = new ArrayList<Integer>(linkedList);
		
		checkSortingPerformance(arrayList);
		checkSortingPerformance(linkedList);

	}

	private static void checkSortingPerformance(List<Integer> list) {
		long listSortingStartTime = System.currentTimeMillis();
		Collections.sort(list);
		long listSortingEndTime = System.currentTimeMillis();
		long listSortingDuration = listSortingEndTime - listSortingStartTime;
		System.out.println(list.getClass().getSimpleName() + " was sorted in " + listSortingDuration);
	}
	
	private static ArrayList<Integer> getRandomArrayList() {
		ArrayList<Integer> result = new ArrayList<Integer>(LIST_SIZE);
		Random random = new Random();
		
		for (int i = 0; i < LIST_SIZE; i++) {
			result.add(random.nextInt());
		}
		
		return result;
	}
	
	private static LinkedList<Integer> getRandomLinkedList() {
		LinkedList<Integer> result = new LinkedList<Integer>();
		Random random = new Random();
		
		for (int i = 0; i < LIST_SIZE; i++) {
			result.add(random.nextInt());
		}
		
		return result;
	}

}
